import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.jdbc.MysqlDataSource;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

class Message {
    public String from;
    public String to;
    public String message;

    @Override
    public String toString() {
        return "Message{" +
                "from='" + from + '\'' +
                ", to='" + to + '\'' +
                ", message='" + message + '\'' +
                '}';
    }
}

@WebServlet("/message")
public class MessageServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    private DataSource dataSource=new MysqlDataSource();


    private void inti(){
    ((MysqlDataSource) dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/message_wall?characterEncoding=utf8&useSSL=false");
    ((MysqlDataSource) dataSource).setUser("root");
    ((MysqlDataSource) dataSource).setPassword("123456");

}
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Message message=objectMapper.readValue(req.getInputStream(),Message.class) ;
        System.out.println("请求中收到的 message: " + message);

        inti();
        String sql="insert into message values(?,?,?)";
        try {
            Connection connection=dataSource.getConnection();
            PreparedStatement statement=connection.prepareStatement(sql);
            statement.setString(1,message.message);
            statement.setString(2,message.message);
            statement.setString(3,message.message);
            statement.executeUpdate();
            statement.close();
            connection.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.setStatus(200);
        resp.getWriter().write("ok");



    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<Message> messageList=new ArrayList<>();
          inti();
          try {
              Connection connection=dataSource.getConnection();
              String sql="select * from message";
           PreparedStatement statement=connection.prepareStatement(sql);

              ResultSet resultSet=  statement.executeQuery();
              while (resultSet.next()){
                  Message message=new Message();
                  message.message=resultSet.getString("message");
                  message.to=resultSet.getString("to");
                  message.from=resultSet.getString("from");
                  messageList.add(message);

              }
              resultSet.close();
              statement.close();
              connection.close();


          } catch (SQLException e) {
              throw new RuntimeException(e);
          }
        String respJson = objectMapper.writeValueAsString(messageList);
        resp.getWriter().write(respJson);
        resp.setStatus(200);
        resp.setContentType("application/json; charset=utf8");

    }
}
